package com.caifeng.mynotepad;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import java.util.List;

public class NotepadAdapter extends BaseAdapter {
    private LayoutInflater layoutInflater;
    private List<NotepadBran> list;

    public NotepadAdapter(Context context, List<NotepadBran> list) {
        this.layoutInflater = LayoutInflater.from(context);
        this.list = list;
    }

    @Override
    public int getCount() {
        return list == null ? 1 : list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    //自定义
    class ViewHolder {
        TextView tvNotepadContent, tvNotepadTime;

        public ViewHolder(View v) {
            tvNotepadContent=v.findViewById(R.id.item_content);
            tvNotepadTime=v.findViewById(R.id.item_date);
        }
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView==null){
            convertView=layoutInflater.inflate(R.layout.note_item,null);
            viewHolder=new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        }else{
            viewHolder=(ViewHolder)convertView.getTag();

        }
        NotepadBran noteInfo=(NotepadBran)getItem(position);
        viewHolder.tvNotepadTime.setText(noteInfo.getNotepadTime());
        viewHolder.tvNotepadContent.setText(noteInfo.getNotepadContent());
        return convertView;
    }
}
